﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyBlog.IService;
using MyBlog.Model;
using MyBlog.WebApi.Utility.ApiResult;

namespace MyBlog.Api.Controllers
{
    /*
     文章类型控制器
     */
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class TypeController : ControllerBase
    {
        #region 固定注入
        private readonly ITypeInfoService _iTypeInfoService;
        public TypeController(ITypeInfoService iTypeInfoService)
        {
            this._iTypeInfoService = iTypeInfoService;
        }
        #endregion

        /// <summary>
        /// 查询全部文章类型[匿名使用]
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("Types")]
        public async Task<ApiResult> Types()
        {
            var types = await _iTypeInfoService.QueryAsync();
            if (types.Count == 0) return ApiResultHelper.Error("没有更多的类型");
            return ApiResultHelper.Success(types);
        }
        /// <summary>
        /// 创建文章类型[鉴权使用]
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost("Create")]
        public async Task<ApiResult> Create(string name)
        {
            // 数据验证
            if (String.IsNullOrWhiteSpace(name)) return ApiResultHelper.Error("文章类型名不能为空");
            
            TypeInfo type = new TypeInfo
            {
                Name = name
            };
            bool b = await _iTypeInfoService.CreateAsync(type);
            if (!b) return ApiResultHelper.Error("添加失败");
            return ApiResultHelper.Success(b);
        }
        /// <summary>
        /// 修改文章类型[鉴权使用]
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPut("Edit")]
        public async Task<ApiResult> Edit(int id, string name)
        {
            var type = await _iTypeInfoService.FindAsync(id);
            if (type == null) return ApiResultHelper.Error("没有找到该文章类型");
            type.Name = name;
            bool b = await _iTypeInfoService.EditAsync(type);
            if (!b) return ApiResultHelper.Error("修改失败");
            return ApiResultHelper.Success(type);
        }
        /// <summary>
        /// 删除文章类型[鉴权使用]
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("Delete")]
        public async Task<ApiResult> Delete(int id)
        {
            bool b = await _iTypeInfoService.DeleteAsync(id);
            if (!b) return ApiResultHelper.Error("删除失败");
            return ApiResultHelper.Success(b);
        }
    }
}
